/**
 * 
 */
package com.nextive.interview.dao.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.nextive.interview.dao.WeightInfoDao;


/**
 * Dao implementation class to obtain information related to weight histories.
 * It extends <code>SqLiteHelperDao</code> class so to access sqlite database.
 * @author Cesar Roman
 *
 */
public class SqLiteWeightInfoDao extends SqLiteHelperDao implements WeightInfoDao {

    // specific database information
	private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "user_weights";
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_DROP = "DROP TABLE IF EXISTS " + DATABASE_TABLE;
    private static final String DATABASE_CREATE =
        "create table " + DATABASE_TABLE + " (" + ID_KEY + " integer primary key autoincrement, "
        + WEIGHT_KEY + " real not null, " 
        + DATE_KEY + " integer not null);";
	
	/**
	 * Default constructor. It receives the application's context
	 * @param context the application's context
	 */
    public SqLiteWeightInfoDao(Context context) {
		
		super(context);
		
	}
	
	/**
	 * It fetches weight history info. It returns as many results as specified by parameter.
	 * @param maxResults if equals to <code>-1</code> then it returns all results
	 * @return cursor containing the query result
	 */
    @Override
	public Cursor fetchWeightHistoriesInfo(int maxResults) {

		return getDatabase().query(true, DATABASE_TABLE, new String[] {ID_KEY, WEIGHT_KEY, DATE_KEY}, 
				null, null, null, null, DATE_KEY + " DESC", maxResults == WeightInfoDao.ALL_RESULTS ? 
				null : String.valueOf(maxResults));

	}
	
	/**
	 * It fetches a single weight history info matching given id.
	 * @param id to look history for
	 * @return cursor containing the query result
	 */
    @Override
	public Cursor fetchWeightHistoryInfo(long id) {
		
		Cursor cursor = getDatabase().query(DATABASE_TABLE, new String[] {ID_KEY, WEIGHT_KEY, DATE_KEY}, ID_KEY + "=" 
				+ id, null, null, null, null);
		
		return cursor;
		
	}
	
	/**
	 * It fetches weight history info since a given date. It matches all 
	 * timestamps greater or equal than given timestamp.
	 * @param timestamp to fetch history since
	 * @return cursor containing the query result
	 */
    @Override
	public Cursor fetchWeightHistoryInfoSince(long timestamp) {
		
		Cursor cursor = getDatabase().query(DATABASE_TABLE, new String[] {
				ID_KEY, WEIGHT_KEY, DATE_KEY}, DATE_KEY + ">=?" 
				, new String[] { new Long(timestamp).toString() }, null, null, DATE_KEY + " DESC");
		
		return cursor;
		
	}

	/**
	 * It deletes a weight history info matching given id.
	 * @param id to delete history
	 * @return true if deleted successfully
	 */
    @Override
	public boolean deleteWeightInfo(long id) {
		
		try {

			return getDatabase().delete(DATABASE_TABLE, ID_KEY + "=?", new String[]{ new Long(id).toString() }) > 0;

		} finally {
			
			close();
			
		}

	}

	/**
	 * It creates a new weight info entry.
	 * @param weight new weight to be added
	 * @param dateMillis new date to be added
	 * @return id the row id of the newly record
	 */
    @Override
	public long createNewWeightInfo(Double weight, long dateMillis) {
		
		try {

			ContentValues values = new ContentValues();
			values.put(WEIGHT_KEY, weight);
			values.put(DATE_KEY, dateMillis);
			
			return getDatabase().insert(DATABASE_TABLE, null, values);

		} finally {
			
			close();
			
		}
		
	}

	/**
	 * It updates an existing weight history info.
	 * @param id the row id of the record to update
	 * @param weight new weight to be updated
	 * @param dateMillis new date to be updated
	 * @return the number of rows affected
	 */
    @Override
	public boolean updateWeightInfo(long id, Double weight, long dateMillis) {
		
		try {

			ContentValues values = new ContentValues();
			values.put(WEIGHT_KEY, weight);
			values.put(DATE_KEY, dateMillis);
			
			return getDatabase().update(DATABASE_TABLE, values, ID_KEY + "=?", new String[]{ new Long(id).toString() }) > 0;

		} finally {
			
			close();
			
		}
	}

	/**
	 * It fetches the database name.
	 * @return db name
	 */
    @Override
	protected String getDBName() {
		
		return DATABASE_NAME;
		
	}

	/**
	 * It fetches the database version.
	 * @return db version
	 */
    @Override
	protected int getDBVersion() {
		
		return DATABASE_VERSION;
		
	}

	/**
	 * It fetches the database create statement.
	 * @return db create statement
	 */
	@Override
	protected String getDBCreateStatement() {
		
		return DATABASE_CREATE;
		
	}

	/**
	 * It fetches the database drop statement.
	 * @return db drop statement
	 */
	@Override
	protected String getDBDropStatement() {
		
		return DATABASE_DROP;
		
	}
	
}